package com.atguigu.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.product.entity.AttrEntity;
import com.atguigu.product.vo.AttrCateVo;
import com.atguigu.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品属性
 *
 * @author litong
 * @email 2943284183@qq.com
 * @date 2024-08-07 18:11:07
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;


    /**
     * 列表
     */
    @GetMapping("/{type}/list/{catId}")
    public R BaseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catId")Long catId,
                      @PathVariable("type") String type){
        System.out.println("+++++");
        PageUtils page = attrService.PageAttrIdAndKey(params,catId,type);
        return R.ok().put("page", page);
    }

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrCateVo attrCateVo = attrService.getAttrAll(attrId);

        return R.ok().put("attr", attrCateVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
//        新增的时候需要新增两个表
		attrService.saveAttrVo(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
